//  https://www.luogu.com.cn/problem/P3146

#include<iostream>
using namespace std;

const int N = 250;
int dp[N][N], a[N];


int main()
{
    int n;cin >> n;
    int ret = 0;
    for (int i = 1;i <= n;i++)
    {
        cin >> a[i];
        dp[i][i] = a[i];
        ret = max(ret, a[i]);
    }
    for (int len = 2;len <= n;len++)
        for (int i = 1;i + len - 1 <= n;i++)
        {
            int j = i + len - 1;
            for (int k = i;k < j;k++)
            {
                if (dp[i][k] && dp[i][k] == dp[k + 1][j])
                    dp[i][j] = max(dp[i][j], dp[i][k] + 1);
            }
            ret = max(ret, dp[i][j]);
        }
    cout << ret;
    return 0;
}